package com.guanghua.brick.html.tag;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.html.CommonDepend;
import com.guanghua.brick.html.IControl;
import com.guanghua.brick.html.IDepend;

public class DependTag extends BodyTagSupport {
	
	private static Log logger = LogFactory.getLog(DependTag.class);
	private IDepend depend = new CommonDepend();
	
	public String getDependId() {
		return depend.getDependId();
	}

	public String getDependName() {
		return depend.getDependName();
	}

	public boolean isWeakDepend() {
		return depend.isWeakDepend();
	}

	public void setDependId(String dependId) {
		depend.setDependId(dependId);
	}

	public void setDependName(String dependName) {
		depend.setDependName(dependName);
	}

	public void setWeakDepend(boolean weakDepend) {
		depend.setWeakDepend(weakDepend);
	}

	public int doEndTag() throws JspException {
		//找到父亲，并add depend
		Tag tag = this.getParent();
		if (tag instanceof IControl) {
			List<IDepend> list = ((IControl)tag).getDepends();
			if (list != null) {
				list.add(depend);
			} else {
				list = new ArrayList<IDepend>();
				((IControl)tag).setDepends(list);
				list.add(depend);
			}
		} else {
			logger.warn("depend parent must be Icontrol");
		}
				
		return BodyTagSupport.EVAL_PAGE;
	}
}
